package com.inputabc.ct.v1.ui.inputbox.component;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

public class InputBoxCloseButtonPanel extends JPanel {
    private JLabel iconLabel;
    
    // 定义配色
    private static final Color BG_COLOR = new Color(228, 78, 78);
    private static final Color BG_HOVER_COLOR = new Color(242, 89, 89);
    private static final Color BG_PRESS_COLOR = new Color(204, 54, 54);
    
    // 当前颜色
    private Color currentBgColor = BG_COLOR;
    
    {
        setName(getClass().getName());
        iconLabel = new JLabel();
    }

    public InputBoxCloseButtonPanel() {
        // 使用更灵活的布局
        setLayout(new GridBagLayout());
        add(iconLabel);
        
        // 确保透明背景
        setOpaque(false);
        
        // 添加鼠标监听器实现悬停效果
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseEntered(MouseEvent e) {
                // 鼠标悬停时改变颜色
                currentBgColor = BG_HOVER_COLOR;
                repaint();
            }

            @Override
            public void mouseExited(MouseEvent e) {
                // 鼠标离开时恢复颜色
                currentBgColor = BG_COLOR;
                repaint();
            }

            @Override
            public void mousePressed(MouseEvent e) {
                // 鼠标按下时改变颜色
                currentBgColor = BG_PRESS_COLOR;
                repaint();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                // 鼠标释放时恢复悬停颜色
                if (contains(e.getPoint())) {
                    currentBgColor = BG_HOVER_COLOR;
                } else {
                    currentBgColor = BG_COLOR;
                }
                repaint();
            }
        });
        
        // 设置鼠标指针为手型，提示可点击
        setCursor(new Cursor(Cursor.HAND_CURSOR));
    }
    
    /**
     * 重写绘制方法，实现圆形按钮效果
     */
    @Override
    protected void paintComponent(Graphics g) {
        Graphics2D g2d = (Graphics2D) g.create();
        
        // 启用抗锯齿
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        
        // 绘制圆形背景
        g2d.setColor(currentBgColor);
        g2d.fill(new RoundRectangle2D.Double(0, 0, getWidth(), getHeight(), getWidth(), getHeight()));
        
        g2d.dispose();
        
        // 调用父类方法绘制其余部分
        super.paintComponent(g);
    }

    public JLabel getIconLabel() {
        return iconLabel;
    }

    public void setIconLabel(JLabel iconLabel) {
        this.iconLabel = iconLabel;
    }
}
